home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Magazin/MacEasy 25
/
Mac Magazin and MacEasy Magazine CD - Issue 25.iso
/
Grafik & Text
/
BibTeX
/
Source code
/
CBibTeXApp.cp
< prev
next >
Wrap
Text File
|
1996-05-31
|
51KB
|
1,021 lines
/**************************************************************************
* *
* Product: Bibtex *
* Class: CBibtexApp *
* Superclass: LDocApplication *
* File: CBibTeXApp.cp *
* Abstract: This file overrides the standard PowerPlant application to *
* provide the features that we need for Bibtex. *
* History: *
* 1.0MI by Rick Zaccone for MakeIndex *
* 1.0 1-Feb-96 VMD. Initial version - basically a global *
* replacement of Rick's 'Makeindex' with 'BibTeX' *
* *
**************************************************************************/
/* There is some old stuff in here from Makeindex which needs tidying up */
#include "CBibTeXApp.h"
#include "BibTeXConstants.h"
#include "BibTeXApp.h"
#include "StdPrefsLib.h"
#include <LEditField.h>
#include <LGrowZone.h>
#include <LWindow.h>
#include "bibutils.h"
#include <PP_Messages.h>
#include <PP_Resources.h>
#include <PPobClasses.h>
#include <string.h>
#include <UDrawingState.h>
#include <UMemoryMgr.h>
#include <URegistrar.h>
// Static data members:
FileFilterUPP CBibtexApp::sGetBtxFileFilter = NULL;
TEHandle CBibtexApp::sTextEditH;
LTextEdit *CBibtexApp::sTextEditPane;
#undef __profile__
#ifdef __profile__
#include <profiler.h>
#pragma profile off
#endif
CBibtexApp* theAppPointer;
/**************************************************************************
* *
* Function: main *
* Purpose: Initializes the heap, the toolbox and installs a grow zone *
* function. Finally, it creates an application object and *
* then runs it. *
* Inputs: None. *
* Outputs: None. *
* Exceptions: None. *
* Side Effects: Creates the application object. *
* Errors: None. *
* History: *
* 1.0 17-Jul-95 RJZ. Initial version. *
* *
**************************************************************************/
void main(void) {
#ifdef __profile__
ProfilerInit(collectDetailed, bestTimeBase, 100, 10);
#endif
// See UDebugging.h for how these
// macros work. Use PP_DebugHeaders68K
// in the C/C++ Language panel when
// debugging and to see error alerts.
// Use PP_ClassHeaders68K when done
// debugging.
SetDebugThrow_(debugAction_Alert);
SetDebugSignal_(debugAction_Alert);
// SetDebugThrow_(debugAction_Nothing);
// SetDebugSignal_(debugAction_Nothing);
// Initialize Memory Manager.
// Parameter is number of Master
// Pointer blocks to allocate
InitializeHeap(3);
// Initialize standard Toolbox managers
UQDGlobals::InitializeToolbox(&qd);
// Install a GrowZone function to catch
// low memory situations.
new LGrowZone(20000);
CBibtexApp theApp;
// so we can get events from outside calls
theAppPointer = &theApp;
theApp.Run();
}
/**************************************************************************
* *
* Method: CBibtexApp *
* Purpose: This is the constructor for the application object. It *
* performs some initialization. If a preferences file *
* exists, it reads it. *
* Inputs: None. *
* Outputs: None. *
* Exceptions: None. *
* Side Effects: Registers all relevant PowerPlant classes. *
* Errors: None. *
* History: *
* 1.0 17-Jul-95 RJZ. Initial version. *
* 1.0.1 27-Oct-95 RJZ. Make the UPP a static data member. *
* 2.0 6-Feb-96 VMD. For BibTeX *
* *
**************************************************************************/
CBibtexApp::CBibtexApp() {
Boolean debugOn;
OSErr err;
short fileRefNum;
// Register functions to create core
// PowerPlant classes
//URegistrar::RegisterClass(LButton::class_ID, LButton::CreateButtonStream);
URegistrar::RegisterClass(LCaption::class_ID, (ClassCreatorFunc)LCaption::CreateCaptionStream);
URegistrar::RegisterClass(LDialogBox::class_ID, (ClassCreatorFunc)LDialogBox::CreateDialogBoxStream);
//URegistrar::RegisterClass(LEditField::class_ID, LEditField::CreateEditFieldStream);
//URegistrar::RegisterClass(LListBox::class_ID, LListBox::CreateListBoxStream);
//URegistrar::RegisterClass(LPane::class_ID, LPane::CreatePaneStream);
//URegistrar::RegisterClass(LPicture::class_ID, LPicture::CreatePictureStream);
//URegistrar::RegisterClass(LPlaceHolder::class_ID, LPlaceHolder::CreatePlaceHolderStream);
//URegistrar::RegisterClass(LPrintout::class_ID, LPrintout::CreatePrintoutStream);
//URegistrar::RegisterClass('prto', LPrintout::CreateOldPrintoutStream);
URegistrar::RegisterClass(LScroller::class_ID, (ClassCreatorFunc)LScroller::CreateScrollerStream);
//URegistrar::RegisterClass(LStdControl::class_ID, LStdControl::CreateStdControlStream);
URegistrar::RegisterClass(LStdButton::class_ID, (ClassCreatorFunc)LStdButton::CreateStdButtonStream);
URegistrar::RegisterClass(LStdCheckBox::class_ID, (ClassCreatorFunc)LStdCheckBox::CreateStdCheckBoxStream);
//URegistrar::RegisterClass(LStdRadioButton::class_ID,LStdRadioButton::CreateStdRadioButtonStream);
//URegistrar::RegisterClass(LStdPopupMenu::class_ID, LStdPopupMenu::CreateStdPopupMenuStream);
URegistrar::RegisterClass(LTextEdit::class_ID, (ClassCreatorFunc)LTextEdit::CreateTextEditStream);
//URegistrar::RegisterClass(LView::class_ID, LView::CreateViewStream);
URegistrar::RegisterClass(LWindow::class_ID, (ClassCreatorFunc)LWindow::CreateWindowStream);
//URegistrar::RegisterClass(LRadioGroup::class_ID, LRadioGroup::CreateRadioGroupStream);
//URegistrar::RegisterClass(LTabGroup::class_ID, LTabGroup::CreateTabGroupStream);
//URegistrar::RegisterClass(LCicnButton::class_ID, LCicnButton::CreateCicnButtonStream);
//URegistrar::RegisterClass(LOffscreenView::class_ID, LOffscreenView::CreateOffscreenViewStream);
//URegistrar::RegisterClass(LActiveScroller::class_ID,LActiveScroller::CreateActiveScrollerStream);
//URegistrar::RegisterClass(LTable::class_ID, LTable::CreateTableStream);
//URegistrar::RegisterClass(LIconPane::class_ID, LIconPane::CreateIconPaneStream);
//URegistrar::RegisterClass(LGroupBox::class_ID, LGroupBox::CreateGroupBoxStream);
//URegistrar::RegisterClass(LTextButton::class_ID, LTextButton::CreateTextButtonStream);
//RegisterAllPPClasses();
// no btx file yet
mGotBtxFile = false;
mLogWindow = NULL;
sGetBtxFileFilter = NewFileFilterProc((ProcPtr) GetBtxFileFilter);
ThrowIfMemError_();
err = ::OneTimeInitialization();
ThrowIfOSErr_(err);
// If the preferences file exists, read
// it and set the appropriate variables
// accordingly.
mPrefsFileExists = PreferencesFileExists(kDummyCreator, kDummyType);
if (mPrefsFileExists) {
err = OpenPreferencesFile(kDummyCreator, kDummyType, &fileRefNum);
if (err == noErr) {
ReadBooleanPref(debugOn, myBLID_Debug, fileRefNum);
ReadBooleanPref(mGoOnOpen, myBLID_GoOnOpen, fileRefNum);
ReadBooleanPref(mAutoQuit, myBLID_AutoQuit, fileRefNum);
ReadBooleanPref(mBigBibTeX, myBLID_BigBibTeX, fileRefNum);
SetBibtexOptions(debugOn);
err = ClosePreferencesFile(fileRefNum);
} else
ReportOSErr("Error trying to open preferences file: %s", err);
// If the preferences file doesn\'t
// exist, use the default values for Go
// On Open and Auto Quit.
} else {
mGoOnOpen = kGoOnOpenDefault;
mAutoQuit = kAutoQuitDefault;
mBigBibTeX = false;
}
}
/*********************************************************************
* *
* Method: ~CBibtexApp *
* Purpose: Destructor for the application object. It doesn't do *
* anything yet. *
* Inputs: None. *
* Outputs: None. *
* Exceptions: None. *
* Side Effects: None. *
* Errors: None. *
* History: *
* 1.0 17-Jul-95 RJZ. Initial version. *
* *
*********************************************************************/
CBibtexApp::~CBibtexApp(void) {
}
/************************************************************************
* *
* Method: StartUp *
* Purpose: This function lets you do something when the application *
* starts up. For example, you could issue your own New *
* command. *
* Inputs: None. *
* Outputs: None. *
* Exceptions: None. *
* Side Effects: None. *
* Errors: None. *
* History: *
* 1.0 17-Jul-95 RJZ. Initial version. *
* *
************************************************************************/
void CBibtexApp::StartUp(void) {
}
/**************************************************************************
* *
* Method: ObeyCommand *
* Purpose: Obeys commands for this application. If the command is *
* not a Bibtex specific command, call the overridden *
* ObeyCommand. *
* Inputs: inCommand is the command to be obeyed. If the command is *
* sent as a result of a menu selection, the ioParam *
* parameter is an Int32*. The value is the result returned *
* by MenuSelect or MenuKey, which has the MENU ID in the hi *
* word and the menu item number in the lo word. This allows *
* you to use a "real" command number rather than relying on *
* synthetic command numbers for menu choices that depend on *
* the text of the menu item. For example, a Font or Size *
* menu. *
* Outputs: ioParam may be an output parameter. See the Inputs *
* section. *
* Exceptions: None. *
* Side Effects: Creates the Bibtex log window. *
* Errors: None. *
* History: *
* 1.0 17-Jul-95 RJZ. Initial version. *
* 1.0.1 23-Oct-95 RJZ. Changed the default so that it calls *
* ObeyCommand for LDocApplication. This allowed *
* me to get rid of the processing for cmd_Open. *
* *
**************************************************************************/
Boolean CBibtexApp::ObeyCommand(CommandT inCommand, void *ioParam) {
Boolean cmdHandled = true;
switch (inCommand) {
case cmd_Go:
ResetWindow();
::InvokeBibtex();
if (mAutoQuit)
ObeyCommand(cmd_Quit, NULL);
break;
case cmd_OpenOptionsDialog:
OpenOptionsDialog();
break;
case cmd_SetOptions:
SetDialogOptions((SDialogResponse *) ioParam);
break;
case cmd_SaveSettings:
DoSaveSettings();
break;
default:
cmdHandled = LDocApplication::ObeyCommand(inCommand, ioParam);
break;
}
return cmdHandled;
}
/**************************************************************************
* *
* Method: FindCommandStatus *
* Purpose: Pass back whether a menu Command is enabled and/or marked. *
* Inputs: inCommand is a menu command number. *
* Outputs: outEnabled indicates if it is enabled, outUsesMark *
* indicates if it is marked, outMark is the mark. outName *
* is the new name for the menu item. *
* Exceptions: None. *
* Side Effects: None. *
* Errors: None. *
* History: *
* 1.0 17-Jul-95 RJZ. Initial version. *
* 1.0.1 23-Oct-95 RJZ. The default case should call *
* LDocApplication::FindCommandStatus(). *
* *
**************************************************************************/
void CBibtexApp::FindCommandStatus(CommandT inCommand,
Boolean &outEnabled,
Boolean &outUsesMark,
Char16 &outMark,
Str255 outName)
{
switch (inCommand) {
// Return menu item status according to
// command messages. Any that you
// don't handle will be passed to
// LDocApplication
case cmd_New:
outEnabled = false; // disable the New command
outUsesMark = false;
break;
// The Open command is available if
// there is no window open and the
// preferences dialog is not open.
case cmd_Open:
outEnabled = !UDesktop::FrontWindowIsModal() && mLogWindow == NULL;
break;
// The Close command is enabled if the
// front window is not modal and there
// is a log window open.
case cmd_Close:
case cmd_Go:
outEnabled = !UDesktop::FrontWindowIsModal() && mLogWindow != NULL;
break;
// The Options... command is available
// as long as the options dialog isn't
// already being displayed. PowerPlant
// will probably do this automatically
// in a future version. Same for Save
// command.
case cmd_OpenOptionsDialog:
case cmd_SaveSettings:
case cmd_Quit:
outEnabled = !UDesktop::FrontWindowIsModal();
break;
default:
LDocApplication::FindCommandStatus(inCommand, outEnabled,
outUsesMark, outMark, outName);
break;
}
}
/**********************************************************************
* *
* Method: CBibtexApp::DisplayInLogWindow (static) *
* Purpose: Displays text in the log window. *
* Inputs: inMessage is a pointer to a string to be displayed. *
* inSize is the size of the string. *
* Outputs: None. *
* Exceptions: None. *
* Side Effects: Changes the contents of the Log Window. *
* Design: Just uses toolbox routines to insert text into the log *
* window. *
* Errors: None. *
* History: *
* 1.0 28-Jul-95 RJZ. Initial version. *
* *
**********************************************************************/
void CBibtexApp::DisplayInLogWindow(char *inMessage, Int32 inSize) {
for(short i=0;i<strlen(inMessage);i++){
if (inMessage[i] == '\n')
inMessage[i] = '\r';
}
::TEInsert(inMessage, inSize, sTextEditH);
::TECalText(sTextEditH);
sTextEditPane->AdjustImageToText();
}
/************************************************************************
* *
* Method: CBibtexApp::PostErrorAlert (static) *
* Purpose: Displays an error Alert. *
* Inputs: inMessage is a pointer to a Pascal style string to be *
* displayed. *
* Outputs: None. *
* Exceptions: None. *
* Side Effects: Displays an alert. *
* Design: Deactivate and activate the desktop. It doesn't seem to *
* be necessary, but all the example programs do it! *
* Errors: None. *
* History: *
* 1.0 28-Jul-95 RJZ. Initial version. *
* *
************************************************************************/
void CBibtexApp::PostErrorAlert(StringPtr inMessage) {
::ParamText(inMessage, "\p", "\p", "\p");
UDesktop::Deactivate();
(void) ::Alert(ALRT_OSErr, NULL);
UDesktop::Activate();
}
/*************************************************************************
* *
* Method: CBibtexApp::OpenOptionsDialog *
* Purpose: Opens the options dialog. Initializes the items in the *
* dialog to reflect the current program settings. *
* Inputs: None. *
* Outputs: None. *
* Exceptions: None. *
* Side Effects: Displays a dialog. *
* Errors: None. *
* History: *
* 1.0 28-Jul-95 RJZ. Initial version. *
* 1.0.1 30-Nov-95 RJZ. Use dynamic_cast to cast pointers. Use *
* Assert_ to assure that it worked. *
* *
*************************************************************************/
void CBibtexApp::OpenOptionsDialog(void) {
LStdCheckBox *checkBox;
Boolean debugOn;
LDialogBox *theDialog;
LUndoer *undoer;
// Create the dialog
theDialog = (LDialogBox *) LWindow::CreateWindow(PPob_OptionsDialog, this);
// make typing undoable
undoer = new LUndoer;
theDialog->AddAttachment(undoer);
// Get current Bibtex settings
::GetBibtexOptions(debugOn);
// Initialize debug box
checkBox = dynamic_cast<LStdCheckBox *>
(theDialog->FindPaneByID(paneID_Debug));
Assert_(checkBox != NULL);
checkBox->SetValue(debugOn);
// Initialize Auto Quit option
checkBox = dynamic_cast<LStdCheckBox *>
(theDialog->FindPaneByID(paneID_AutoQuit));
Assert_(checkBox != NULL);
checkBox->SetValue(mAutoQuit);
// Initialize Go On Open option
checkBox = dynamic_cast<LStdCheckBox *>
(theDialog->FindPaneByID(paneID_GoOnOpen));
Assert_(checkBox != NULL);
checkBox->SetValue(mGoOnOpen);
checkBox = dynamic_cast<LStdCheckBox *>
(theDialog->FindPaneByID(paneID_BigBibTeX));
Assert_(checkBox != NULL);
checkBox->SetValue(mBigBibTeX);
theDialog->Show();
}
/*************************************************************************
* *
* Method: CBibtexApp::SetDialogOptions *
* Purpose: This function gets invoked when the user hits the OK *
* button. It extracts the selections the user has made and *
* sets Bibtex variables accordingly. *
* Inputs: inPresponse is a pointer to a SDialogResponse structure. *
* Outputs: None. *
* Exceptions: None. *
* Side Effects: May change the value of some instance variables. *
* Errors: None. *
* History: *
* 1.0 28-Jul-95 RJZ. Initial version. *
* 1.0.1 30-Nov-95 RJZ. Use dynamic_cast to cast pointers. *
* Assert_ that it worked. *
* *
*************************************************************************/
void CBibtexApp::SetDialogOptions(SDialogResponse *inResponse) {
LStdCheckBox *checkBox;
Boolean debugOn;
// Get value of debug box
checkBox = dynamic_cast<LStdCheckBox *>
(inResponse->dialogBox->FindPaneByID(paneID_Debug));
Assert_(checkBox != NULL);
debugOn = checkBox->GetValue();
// Get value of auto quit box
checkBox = dynamic_cast<LStdCheckBox *>
(inResponse->dialogBox->FindPaneByID(paneID_AutoQuit));
Assert_(checkBox != NULL);
mAutoQuit = checkBox->GetValue();
// Get value of Go On Open box
checkBox = dynamic_cast<LStdCheckBox *>
(inResponse->dialogBox->FindPaneByID(paneID_GoOnOpen));
Assert_(checkBox != NULL);
mGoOnOpen = checkBox->GetValue();
checkBox = dynamic_cast<LStdCheckBox *>
(inResponse->dialogBox->FindPaneByID(paneID_BigBibTeX));
Assert_(checkBox != NULL);
mBigBibTeX = checkBox->GetValue();
// Set options
::SetBibtexOptions(debugOn);
// Delete the dialog box
delete inResponse->dialogBox;
}
/**************************************************************************
* *
* Method: CBibtexApp::AllowSubRemoval *
* Purpose: The window object invokes this method before closing the *
* Log Window. This allows us to do any cleaning up we need *
* to do. *
* Inputs: inSub (I forget what it's for!) *
* Outputs: Returns true indicating that it's OK to close the window. *
* Exceptions: None. *
* Side Effects: Sets mLogWindow to indicate that the log window is no *
* longer visible. Also closes any files that might be open. *
* Errors: None. *
* History: *
* 1.0 28-Jul-95 RJZ. Initial version. *
* *
**************************************************************************/
Boolean CBibtexApp::AllowSubRemoval(LCommander *inSub) {
#pragma unused (inSub)
mLogWindow = NULL;
::CloseAllFiles();
return true;
}
/************************************************************************
* *
* Method: CBibtexApp::MakeNewDocument *
* Purpose: This function gets invoked if the user wants to create a *
* new document. This is not a feature we support. *
* Inputs: None. *
* Outputs: Just return NULL. *
* Exceptions: None. *
* Side Effects: None. *
* Errors: None. *
* History: *
* 1.0 28-Jul-95 RJZ. Initial version. *
* *
************************************************************************/
LModelObject *CBibtexApp::MakeNewDocument(void) {
return NULL;
}
/**************************************************************************
* *
* Method: CBibtexApp::ChooseDocument *
* Purpose: Asks the user to select a document to open. We filter the *
* names so that only text files that end in ".aux" are *
* shown. If the user makes a selection, open the file. *
* Inputs: None. *
* Outputs: None. *
* Exceptions: None. *
* Side Effects: Displays a standard open file dialog on the screen. *
* Errors: None. *
* History: *
* 1.0 28-Jul-95 RJZ. Initial version. *
* 1.0.1 27-Oct-95 RJZ. Make the UPP a static data member. *
* *
**************************************************************************/
void CBibtexApp::ChooseDocument(void) {
StandardFileReply reply;
SFTypeList typeList;
typeList[0] = 'TEXT';
UDesktop::Deactivate();
::StandardGetFile(sGetBtxFileFilter, 1, typeList, &reply);
UDesktop::Activate();
if (reply.sfGood)
OpenDocument(&reply.sfFile);
}
/*************************************************************************
* *
* Function: CBibtexApp::GetBtxFileFilter (static) *
* Purpose: Filters file names for StandardGetFile so that the user *
* will see only names that end in .aux. *
* Inputs: pb is a pointer to an information block for the name of a *
* file that StandardGetFile is considering to display. *
* Outputs: True if StandardGetFile should filter the file (not *
* display it), false to display the file. *
* Exceptions: None. *
* Side Effects: Affects which files are displayed by StandardGetFile. *
* Design: If the file name is longer than 4 characters, assume that *
* the last 4 characters contain an extension. *
* Errors: None. *
* History: *
* 1.0 10-Jul-95 RJZ. Initial version. *
* 1.0.1 27-Oct-95 RJZ. This is now a static member function. *
* *
*************************************************************************/
pascal Boolean CBibtexApp::GetBtxFileFilter(CInfoPBPtr pb) {
char *extensionPtr;
Boolean filter;
StringPtr name;
name = pb->hFileInfo.ioNamePtr;
extensionPtr = (char *) name + StrLength(name) - 3;
if (StrLength(name) > 4 && strncmp(extensionPtr, BIBTEX_INP, 4) == 0)
filter = false;
else
filter = true;
return filter;
}
/**************************************************************************
* *
* Method: CBibtexApp::OpenDocument *
* Purpose: Performs all processing associated with opening a *
* document. It tells Bibtex about the file. If a window *
* is already open, it clears out the text and changes the *
* window title. If no window is open, it creates a new one *
* and displays it. *
* Inputs: inMacFSSpec is a pointer to the FSSpec record of the file *
* to be opened. *
* Outputs: None. *
* Exceptions: None. *
* Side Effects: Initializes some instance variables such as *
* mGotBtxFile, mLogWindow, sTextEditPane, and sTextEditH. *
* Errors: None. *
* History: *
* 1.0 28-Jul-95 RJZ. Initial version. *
* *
**************************************************************************/
void CBibtexApp::OpenDocument(FSSpec *inMacFSSpec) {
::CloseAllFiles();
::IntroduceBtxFile(inMacFSSpec);
mGotBtxFile = true;
if (mGoOnOpen) {
ObeyCommand(cmd_Go, NULL);
} else {
ResetWindow();
}
}
/* Separated this from above function so 'Go' can call it too */
/* VMD May31st 1996 */
void CBibtexApp::ResetWindow(void){
Str255 windowTitle;
if (mLogWindow != NULL) {
::TESetSelect(0L, 32767L, sTextEditH);
::TEDelete(sTextEditH);
::GetWindowTitle(windowTitle);
mLogWindow->SetDescriptor(windowTitle);
mLogWindow->Refresh();
} else {
mLogWindow = LWindow::CreateWindow(PPob_EditWindow, this);
::GetWindowTitle(windowTitle);
mLogWindow->SetDescriptor(windowTitle);
mLogWindow->Show();
sTextEditPane = (LTextEdit *)
mLogWindow->FindPaneByID(paneID_TextEdit);
sTextEditH = sTextEditPane->GetMacTEH();
}
}
/***********************************************************************
* *
* Method: CBibtexApp::DoSaveSettings *
* Purpose: This function gets invoked when the user selects Save *
* Current Settings from the Options menu. It saves the *
* current settings in the preferences file. If the *
* preferences file doesn't exist, it creates it. *
* Inputs: None. *
* Outputs: None. *
* Exceptions: None. *
* Side Effects: Write the current settings to the preferences file. *
* May also create a preferences file. *
* Errors: None. *
* History: *
* 1.0 28-Jul-95 RJZ. Initial version. *
* *
***********************************************************************/
void CBibtexApp::DoSaveSettings(void) {
Boolean debugOn;
OSErr err;
short fileRefNum;
if (!mPrefsFileExists)
CreatePrefsFile();
// Get current Bibtex settings
if (mPrefsFileExists) {
::GetBibtexOptions(debugOn);
err = ::OpenPreferencesFile(kDummyCreator, kDummyType, &fileRefNum);
if (err == noErr) {
WriteBooleanPref(debugOn, myBLID_Debug, fileRefNum);
WriteBooleanPref(mGoOnOpen, myBLID_GoOnOpen, fileRefNum);
WriteBooleanPref(mAutoQuit, myBLID_AutoQuit, fileRefNum);
WriteBooleanPref(mBigBibTeX, myBLID_BigBibTeX, fileRefNum);
err = ::ClosePreferencesFile(fileRefNum);
} else
::ReportOSErr("Error trying to open preferences file: %s", err);
}
}
/**************************************************************************
* *
* Method: CBibtexApp::CreatePrefsFile *
* Purpose: Creates a preferences file. *
* Inputs: None. *
* Outputs: None. *
* Exceptions: None. *
* Side Effects: Creates a preferences file. *
* Design: This function uses the StdPrefsLib to do all the right *
* things. It follows the guidelines in Develop 18's article *
* "The Right Way to Implement Preferences Files." It even *
* creates two vers resources for the file and inserts the *
* application-missing resource ('STR ' -16397). *
* Errors: Posts an alert if there are any troubles creating the *
* file. *
* History: *
* 1.0 28-Jul-95 RJZ. Initial version. *
* *
**************************************************************************/
void CBibtexApp::CreatePrefsFile(void) {
Str255 companyStr = kCompanyString;
OSErr err;
short fileRefNum;
Str255 fileSetStr = kFileSetString;
NumVersion numVersion;
short regionCode;
Str255 versNumStr = kVersNumString;
err = ::NewPreferencesFile(kDummyCreator, kDummyType,
kPrefFileName, NULL, kIdentString);
if (err == noErr) {
mPrefsFileExists = true;
numVersion.majorRev = kMajorRevision;
numVersion.minorAndBugRev = kMinorAndBugRevision;
numVersion.stage = kStage;
numVersion.nonRelRev = kNonReleaseRevision;
regionCode = ::GetScriptManagerVariable(smRegionCode);
err = ::OpenPreferencesFile(kDummyCreator, kDummyType, &fileRefNum);
}
// Set the contents of the vers 1
// resource
if (err == noErr)
err = ::SetPreferencesFileVersion(fileRefNum, kVers1, &numVersion,
regionCode, versNumStr, companyStr);
// Set the contents of the vers 2
// resource
if (err == noErr)
err = ::SetPreferencesFileVersion(fileRefNum, kVers2, &numVersion,
regionCode, versNumStr, fileSetStr);
// Post an alert if error
if (err != noErr)
::ReportOSErr("Error trying to create Preferences file: %s", err);
// Close prefs file
err = ::ClosePreferencesFile(fileRefNum);
}
/*************************************************************************
* *
* Method: CBibtexApp::WriteBooleanPref *
* Purpose: Writes a Boolean preference to the preferences file. *
* Boolean preferences are stored in myBL resources. *
* Inputs: inValue is the value to be saved. inID the the id of the *
* myBL resource to use. *
* Outputs: None. *
* Exceptions: None. *
* Side Effects: Writes to the preferences file. *
* Errors: Posts an alert if there was trouble writing. *
* History: *
* 1.0 28-Jul-95 RJZ. Initial version. *
* *
*************************************************************************/
void CBibtexApp::WriteBooleanPref(Boolean inValue,
short inID,
short inFileRefNum)
{
OSErr err = noErr;
Handle prefHandle;
short resID;
resID = inID;
// Create a handle for our preference
// data
prefHandle = ::NewHandle(sizeof(char));
if (prefHandle == NULL)
err = ::MemError();
// Set the contents of the preference
// data
if (err == noErr) {
**((char **) prefHandle) = inValue;
// Write the preference data to prefs
// file
err = ::WritePreference(inFileRefNum, 'myBL', &resID, prefHandle);
}
// Post an alert if error
if (err != noErr)
::ReportOSErr("Error trying to write Preferences file: %s", err);
// clean up
::DisposeHandle(prefHandle);
}
/**************************************************************************
* *
* Method: CBibtexApp::WriteLongPref *
* Purpose: Writes a long preference to the preferences file *
* Inputs: inValue is the value to be written. inID is the id of the *
* myLN resource to use. *
* Outputs: None. *
* Exceptions: None. *
* Side Effects: Writes to the preferences file. *
* Errors: Posts an alert if there is an error. *
* History: *
* 1.0 28-Jul-95 RJZ. Initial version. *
* *
**************************************************************************/
void CBibtexApp::WriteLongPref(long inValue,
short inID,
short inFileRefNum)
{
OSErr err = noErr;
Handle prefHandle;
short resID;
resID = inID;
// Create a handle for our preference
// data
prefHandle = ::NewHandle(sizeof(long));
if (prefHandle == NULL)
err = ::MemError();
// Set the contents of the preference
// data
if (err == noErr) {
**((long **) prefHandle) = inValue;
// Write the preference data to prefs
// file
err = ::WritePreference(inFileRefNum, 'myLN', &resID, prefHandle);
}
// Post an alert if error
if (err != noErr)
::ReportOSErr("Error trying to write Preferences file: %s", err);
// clean up
::DisposeHandle(prefHandle);
}
/*************************************************************************
* *
* Method: CBibtexApp::WriteStringPref *
* Purpose: Writes a string preference to the preferences file. *
* Inputs: inValue is a pointer to a Pascal style string to be *
* written. It may be any length up to 255. inID is the id *
* of the myST resource to use. *
* Outputs: None. *
* Exceptions: None. *
* Side Effects: Writes to the preferences file. *
* Errors: Posts an alert if there is an error. *
* History: *
* 1.0 28-Jul-95 RJZ. Initial version. *
* *
*************************************************************************/
void CBibtexApp::WriteStringPref(StringPtr inValue,
short inID,
short inFileRefNum)
{
Size byteCount;
OSErr err = noErr;
Handle prefHandle;
short resID;
resID = inID;
// Create a handle for our preference
// data
prefHandle = ::NewHandleClear(sizeof(Str255));
if (prefHandle == NULL)
err = ::MemError();
// Set the contents of the preference
// data. It\'s probably not necessary
// to lock the handle. Be safe though.
if (err == noErr) {
byteCount = inValue[0] + 1;
::HLock(prefHandle);
::BlockMoveData(inValue, *prefHandle, byteCount);
::HUnlock(prefHandle);
// Write the preference data to prefs
// file
err = ::WritePreference(inFileRefNum, 'myST', &resID, prefHandle);
}
// Post an alert if error
if (err != noErr)
::ReportOSErr("Error trying to write Preferences file: %s", err);
// clean up
::DisposeHandle(prefHandle);
}
/*********************************************************************
* *
* Method: CBibtexApp::ReadBooleanPref *
* Purpose: Reads a Boolean preference from the preferences file. *
* Inputs: inID is the id of the myBL resource to read. *
* Outputs: outValue is the value of the resource. *
* Exceptions: None. *
* Side Effects: None. *
* Errors: Posts an alert if there is an error. *
* History: *
* 1.0 28-Jul-95 RJZ. Initial version. *
* *
*********************************************************************/
void CBibtexApp::ReadBooleanPref(Boolean& outValue,
short inID,
short inFileRefNum)
{
OSErr err;
Handle prefHandle;
short resID;
resID = inID;
// Read the resource
err = ::ReadPreference(inFileRefNum, 'myBL', &resID, &prefHandle);
// Post an alert if error
if (err != noErr)
::ReportOSErr("Error trying to read Preferences file: %s", err);
outValue = **prefHandle != 0;
::DisposeHandle(prefHandle);
}
/***************************************************************
* *
* Method: CBibtexApp::ReadLongPref *
* Purpose: Reads a long value from a myLN resource. *
* Inputs: inID is the id of the myLN resource to read. *
* Outputs: outValue is the value read. *
* Exceptions: None. *
* Side Effects: None. *
* Errors: Posts an alert if there is any trouble reading. *
* History: *
* 1.0 28-Jul-95 RJZ. Initial version. *
* *
***************************************************************/
void CBibtexApp::ReadLongPref(long& outValue,
short inID,
short inFileRefNum)
{
OSErr err;
Handle prefHandle;
short resID;
resID = inID;
// Read the resource
err = ::ReadPreference(inFileRefNum, 'myLN', &resID, &prefHandle);
// Post an alert if error
if (err != noErr)
::ReportOSErr("Error trying to read Preferences file: %s", err);
outValue = **((long **) prefHandle);
::DisposeHandle(prefHandle);
}
/*******************************************************************
* *
* Method: CBibtexApp::ReadStringPref *
* Purpose: Reads a string preference from the preference file. *
* Inputs: inID is the id of the myST resource to read. *
* Outputs: outValue points to the value read. *
* Exceptions: None *
* Side Effects: None. *
* Errors: Posts an alert if there is a read error. *
* History: *
* 1.0 28-Jul-95 RJZ. Initial version. *
* *
*******************************************************************/
void CBibtexApp::ReadStringPref(StringPtr outValue,
short inID,
short inFileRefNum)
{
Size byteCount;
OSErr err;
Handle prefHandle;
short resID;
resID = inID;
// Read the resource
err = ::ReadPreference(inFileRefNum, 'myST', &resID, &prefHandle);
// Post an alert if error
if (err != noErr)
::ReportOSErr("Error trying to read Preferences file: %s", err);
::HLock(prefHandle);
byteCount = **prefHandle + 1;
::BlockMoveData(*prefHandle, outValue, byteCount);
::DisposeHandle(prefHandle);
}
#pragma profile reset